package org.example.src.service.impl;

import org.example.src.dto.request.WeeklyReportRequest;
import org.example.src.dto.response.WeeklyReportResponse;
import org.example.src.entity.Application;
import org.example.src.entity.Student;
import org.example.src.entity.WeeklyReport;
import org.example.src.repository.ApplicationRepository;
import org.example.src.repository.StudentRepository;
import org.example.src.repository.WeeklyReportRepository;
import org.example.src.service.WeeklyReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WeeklyReportServiceImpl implements WeeklyReportService {

    @Autowired
    private WeeklyReportRepository weeklyReportRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private ApplicationRepository applicationRepository;

    @Override
    public List<WeeklyReport> findAllWeeklyReports() {
        return weeklyReportRepository.findAll();
    }

    @Override
    public WeeklyReport findWeeklyReportById(Long id) {
        return weeklyReportRepository.findById(id).orElse(null);
    }

    @Override
    public WeeklyReport createWeeklyReport(WeeklyReportRequest request) {
        Student student = studentRepository.findById(request.getStudentId())
                .orElseThrow(() -> new RuntimeException("学生未找到"));

        WeeklyReport weeklyReport = new WeeklyReport();
        weeklyReport.setTitle(request.getTitle());
        weeklyReport.setContent(request.getContent());
        weeklyReport.setWeekNumber(request.getWeekNumber());
        weeklyReport.setStartDate(request.getStartDate());
        weeklyReport.setEndDate(request.getEndDate());
        weeklyReport.setStudent(student);

        if (request.getApplicationId() != null) {
            Application application = applicationRepository.findById(request.getApplicationId())
                    .orElseThrow(() -> new RuntimeException("申请未找到"));
            weeklyReport.setApplication(application);
        }

        return weeklyReportRepository.save(weeklyReport);
    }

    @Override
    public WeeklyReport updateWeeklyReport(Long id, WeeklyReportRequest request) {
        WeeklyReport weeklyReport = weeklyReportRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("周报未找到"));

        weeklyReport.setTitle(request.getTitle());
        weeklyReport.setContent(request.getContent());
        weeklyReport.setWeekNumber(request.getWeekNumber());
        weeklyReport.setStartDate(request.getStartDate());
        weeklyReport.setEndDate(request.getEndDate());

        if (request.getApplicationId() != null) {
            Application application = applicationRepository.findById(request.getApplicationId())
                    .orElseThrow(() -> new RuntimeException("申请未找到"));
            weeklyReport.setApplication(application);
        }

        return weeklyReportRepository.save(weeklyReport);
    }

    @Override
    public void deleteWeeklyReport(Long id) {
        weeklyReportRepository.deleteById(id);
    }

    @Override
    public List<WeeklyReport> findWeeklyReportsByStudentId(Long studentId) {
        return weeklyReportRepository.findByStudentId(studentId);
    }

    @Override
    public List<WeeklyReport> findWeeklyReportsByApplicationId(Long applicationId) {
        return weeklyReportRepository.findByApplicationId(applicationId);
    }

    @Override
    public List<WeeklyReportResponse> getWeeklyReportsByStudentId(Long studentId) {
        List<WeeklyReport> weeklyReports = weeklyReportRepository.findByStudentId(studentId);
        return weeklyReports.stream().map(this::convertToResponse).collect(Collectors.toList());
    }

    private WeeklyReportResponse convertToResponse(WeeklyReport weeklyReport) {
        WeeklyReportResponse response = new WeeklyReportResponse();
        BeanUtils.copyProperties(weeklyReport, response);

        if (weeklyReport.getStudent() != null) {
            response.setStudentId(weeklyReport.getStudent().getId());
            response.setStudentName(weeklyReport.getStudent().getFullName());
        }

        if (weeklyReport.getApplication() != null) {
            response.setApplicationId(weeklyReport.getApplication().getId());
        }

        return response;
    }
}
